package com.psct.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.psct.Constant;
import com.psct.R;
import com.psct.data.PSCTSettingSave;
import com.psct.music.MusicManager;
import com.psct.wiget.Switch;



public class SettingActivity extends SherlockActivity implements OnCheckedChangeListener{
	public static final String TAG="setting";
	private Switch sc_music, sc_update;
	private PSCTSettingSave setting;
	private RadioGroup rgBgMusicModel;
	private LinearLayout controlLayout ;
	/**
	 * 歌单管理
	 */
	private TextView tvMusicManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActionBar actionBar = this.getSupportActionBar();
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.actionbar_bg));
		this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		this.setContentView(R.layout.activity_setting);
		setting = new PSCTSettingSave(this);
		controlLayout = (LinearLayout) this.findViewById(R.id.container_music_setting);
		controlLayout.setVisibility(setting.isMusicAutoPlay()?View.VISIBLE:View.GONE);
		sc_music = (Switch) this.findViewById(R.id.switchBtn);
		sc_music.setChecked(setting.isMusicAutoPlay());
		sc_music.setOnCheckedChangeListener(this);
		sc_update = (Switch) this.findViewById(R.id.switchBtn2);
		sc_update.setChecked(setting.isAutoUpdate());
		sc_update.setOnCheckedChangeListener(this);
		rgBgMusicModel = (RadioGroup) this.findViewById(R.id.rg_bgMusic_model);
		int checkedId = getSavedModel();
		rgBgMusicModel.check(checkedId);
		rgBgMusicModel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				Log.v(TAG, "checkedId:"+checkedId+"");
				switch(checkedId) {
				case R.id.rb_nomal:
					setting.savaPlayModel(Constant.MODEL_NOMAL);
					MusicManager.getInstance().setMode(Constant.MODEL_NOMAL);
					break;
				case R.id.rb_local:
					setting.savaPlayModel(Constant.MODEL_SDCARD);
					MusicManager.getInstance().setMode(Constant.MODEL_SDCARD);
					break;
				case R.id.rb_net:
					setting.savaPlayModel(Constant.MODEL_NET);
					MusicManager.getInstance().setMode(Constant.MODEL_NET);
					break;
				}
				
			}
			
		});
		
		tvMusicManager = (TextView) this.findViewById(R.id.tv_music_list_manage);
		tvMusicManager.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
			}
			
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	/**获得播放模式
	 * @return
	 */
	protected int getSavedModel() {
		int checkedId = R.id.rb_nomal;
		switch(setting.getPlayModel()){
		case Constant.MODEL_NET:
			checkedId=R.id.rb_net;
			break;
		case Constant.MODEL_NOMAL:
			checkedId=R.id.rb_nomal;
			break;
		case Constant.MODEL_SDCARD:
			checkedId=R.id.rb_local;
			break;
		}
		return checkedId;
	}

	/**
	 * switcher的事件监听 
	 */
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.switchBtn:// 背景音乐
			setting.saveMusicAutoPlay(isChecked);
			if(isChecked) {
				MusicManager.getInstance().setMode(setting.getPlayModel());
				MusicManager.getInstance().play();
				controlLayout.setVisibility(View.VISIBLE);
			}else {
				MusicManager.getInstance().stopService();
				controlLayout.setVisibility(View.GONE);
			}
			break;
		case R.id.switchBtn2:// 自动更新
			setting.saveAutoUpdate(isChecked);
			break;

		default:
			break;
		}
	}

}
